<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生" prop="doctorId">
        <el-input
          v-model="queryParams.doctorId"
          placeholder="请输入医生"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="画课日期" prop="courseDate">
        <el-date-picker clearable
                        v-model="queryParams.courseDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择画课日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="画课项目" prop="courseProject">
        <el-input
          v-model="queryParams.courseProject"
          placeholder="请输入画课项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="画课时长" prop="courseHour">
        <el-input
          v-model="queryParams.courseHour"
          placeholder="请输入画课时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="false">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yushi:course:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yushi:course:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yushi:course:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['yushi:course:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="医生姓名" align="center" prop="doctorName" />
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="画课日期" align="center" prop="courseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.courseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="画课项目名称" align="center" prop="courseProject" />
      <el-table-column label="画课时长" align="center" prop="courseHour" />
      <el-table-column label="画课状态" prop="courseStatus">
        <template v-slot="scope">
          <span v-if="scope.row.courseStatus == 1" style="color: orange">待确认</span>
          <span v-if="scope.row.courseStatus == 2" style="color: green">患者确认</span>
          <span v-if="scope.row.courseStatus == 3" style="color: green">自动确认</span>
        </template>
      </el-table-column>
      <el-table-column label="确认时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改画课记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医生用户ID" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入医生用户ID" />
        </el-form-item>
        <el-form-item label="患者用户ID" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者用户ID" />
        </el-form-item>
        <el-form-item label="画课日期" prop="courseDate">
          <el-date-picker clearable
                          v-model="form.courseDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择画课日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="画课项目名称" prop="courseProject">
          <el-input v-model="form.courseProject" placeholder="请输入画课项目名称" />
        </el-form-item>
        <el-form-item label="画课时长" prop="courseHour">
          <el-input v-model="form.courseHour" placeholder="请输入画课时长" />
        </el-form-item>
        <el-form-item label="画课确认时间" prop="confirmTime">
          <el-date-picker clearable
                          v-model="form.confirmTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择画课确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="画课确认用户ID" prop="confirmBy">
          <el-input v-model="form.confirmBy" placeholder="请输入画课确认用户ID" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCourse, getCourse, delCourse, addCourse, updateCourse } from "@/api/yushi/course"

export default {
  name: "Course",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 画课记录表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: null,
        patientId: null,
        courseDate: null,
        courseProject: null,
        courseHour: null,
        courseStatus: null,
        confirmTime: null,
        confirmBy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    const userId = this.$route.query && this.$route.query.userId
    this.getList()
  },
  methods: {
    /** 查询画课记录列表 */
    getList() {
      this.loading = true
      listCourse(this.queryParams).then(response => {
        this.courseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        courseId: null,
        doctorId: null,
        patientId: null,
        courseDate: null,
        courseProject: null,
        courseHour: null,
        courseStatus: null,
        confirmTime: null,
        confirmBy: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.courseId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加画课记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const courseId = row.courseId || this.ids
      getCourse(courseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改画课记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != null) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const courseIds = row.courseId || this.ids
      this.$modal.confirm('是否确认删除画课记录编号为"' + courseIds + '"的数据项？').then(function () {
        return delCourse(courseIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('yushi/course/export', {
        ...this.queryParams
      }, `course_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
